/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ecy.quadratic.manager;

import android.text.TextUtils;

import com.ecy.quadratic.beans.ComicDetail;
import com.ecy.quadratic.reader.base.Constant;
import com.ecy.quadratic.reader.utils.ACache;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏列表管理
 * Created by lfh on 2016/9/22.
 */
public class ComicCollectManager {

    private volatile static ComicCollectManager singleton;

    private ComicCollectManager() {

    }

    public static ComicCollectManager getInstance() {
        if (singleton == null) {
            synchronized (ComicCollectManager.class) {
                if (singleton == null) {
                    singleton = new ComicCollectManager();
                }
            }
        }
        return singleton;
    }

    /**
     * 获取浏览历史
     *
     * @return
     */
    public List<ComicDetail> getCollectList() {
        List<ComicDetail> list = (ArrayList<ComicDetail>) ACache.get(new File(Constant.PATH_COMIC_COLLECT)).getAsObject("collect");
        return list == null ? null : list;
    }

    public void putCollectList(List<ComicDetail> list) {
        ACache.get(new File(Constant.PATH_COMIC_COLLECT)).put("collect", (Serializable) list);
    }

//    /**
//     * 按排序方式获取收藏列表
//     *
//     * @return
//     */
//    public List<ComicBroswer> getBroswerListBySort() {
//        List<ComicBroswer> list = getBroswerList();
//        if (list == null) {
//            return null;
//        } else {
//            if (SharedPreferencesUtil.getInstance().getBoolean(Constant.ISBYUPDATESORT, true)) {
//                Collections.sort(list, new LatelyUpdateTimeComparator());
//            } else {
//                Collections.sort(list, new RecentReadingTimeComparator());
//            }
//            return list;
//        }
//    }

    /**
     * 移除单个浏览历史
     *
     * @param comicId
     */
    public void remove(String comicId) {
        List<ComicDetail> list = getCollectList();
        if (list == null) {
            return;
        }
        for (ComicDetail bean : list) {
            if (TextUtils.equals(bean.getId(), comicId)) {
                list.remove(bean);
                putCollectList(list);
                break;
            }
        }
        EventManager.refreshCollectionList();
    }

    /**
     * 是否已收藏
     *
     * @param bookId
     * @return
     */
    public boolean isCollected(String bookId) {
        List<ComicDetail> list = getCollectList();
        if (list == null || list.isEmpty()) {
            return false;
        }
        for (ComicDetail bean : list) {
            if (bean.getId().equals(bookId)) {
                return true;
            }
        }
        return false;
    }

//    /**
//     * 是否已置顶
//     *
//     * @param bookId
//     * @return
//     */
//    public boolean isTop(String bookId) {
//        List<Recommend.RecommendBooks> list = getCollectionList();
//        if (list == null || list.isEmpty()) {
//            return false;
//        }
//        for (Recommend.RecommendBooks bean : list) {
//            if (bean._id.equals(bookId)) {
//                if (bean.isTop)
//                    return true;
//            }
//        }
//        return false;
//    }

//    /**
//     * 移除多个收藏
//     *
//     * @param removeList
//     */
//    public void removeSome(List<Recommend.RecommendBooks> removeList, boolean removeCache) {
//        List<ComicBroswer> list = getBroswerList();
//        if (list == null) {
//            return;
//        }
//        if (removeCache) {
//            for (Recommend.RecommendBooks book : removeList) {
//                try {
//                    // 移除章节文件
//                    FileUtils.deleteFileOrDirectory(FileUtils.getBookDir(book._id));
//                    // 移除目录缓存
//                    CacheManager.getInstance().removeTocList(AppUtils.getAppContext(), book._id);
//                    // 移除阅读进度
//                    SettingManager.getInstance().removeReadProgress(book._id);
//                } catch (IOException e) {
//                    LogUtils.e(e.toString());
//                }
//            }
//        }
//        list.removeAll(removeList);
//        putCollectionList(list);
//    }

    /**
     * 加入收藏
     *
     * @param comicDetail
     */
    public boolean add(ComicDetail comicDetail) {
        List<ComicDetail> list = getCollectList();
        if (list == null) {
            list = new ArrayList<>();
        }
        for (ComicDetail bean : list) {
            if (TextUtils.equals(comicDetail.getId(), bean.getId())) {
                list.remove(bean);
                list.add(0, comicDetail);
                putCollectList(list);
                EventManager.refreshComicCollect();
                return true;
            }
        }
        list.add(0, comicDetail);
        putCollectList(list);
        EventManager.refreshComicCollect();
        return true;
    }

//    /**
//     * 设置最新章节和更新时间
//     *
//     * @param bookId
//     */
//    public synchronized void setLastChapterAndLatelyUpdate(String bookId, String lastChapter, String latelyUpdate) {
//        List<Recommend.RecommendBooks> list = getCollectionList();
//        if (list == null) {
//            return;
//        }
//        for (Recommend.RecommendBooks bean : list) {
//            if (TextUtils.equals(bean._id, bookId)) {
//                bean.lastChapter = lastChapter;
//                bean.updated = latelyUpdate;
//                list.remove(bean);
//                list.add(bean);
//                putCollectionList(list);
//                break;
//            }
//        }
//    }
//
//    /**
//     * 设置最近阅读时间
//     *
//     * @param bookId
//     */
//    public void setRecentReadingTime(String bookId) {
//        List<Recommend.RecommendBooks> list = getCollectionList();
//        if (list == null) {
//            return;
//        }
//        for (Recommend.RecommendBooks bean : list) {
//            if (TextUtils.equals(bean._id, bookId)) {
//                bean.recentReadingTime = FormatUtils.getCurrentTimeString(FormatUtils.FORMAT_DATE_TIME);
//                list.remove(bean);
//                list.add(bean);
//                putCollectionList(list);
//                break;
//            }
//        }
//    }
//
//    public void clear() {
//        try {
//            FileUtils.deleteFileOrDirectory(new File(Constant.PATH_COMIC_BROSWER));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    /**
//     * 自定义比较器：按最近阅读时间来排序，置顶优先，降序
//     */
//    static class RecentReadingTimeComparator implements Comparator {
//        public int compare(Object object1, Object object2) {
//            Recommend.RecommendBooks p1 = (Recommend.RecommendBooks) object1;
//            Recommend.RecommendBooks p2 = (Recommend.RecommendBooks) object2;
//            if (p1.isTop && p2.isTop || !p1.isTop && !p2.isTop) {
//                return p2.recentReadingTime.compareTo(p1.recentReadingTime);
//            } else {
//                return p1.isTop ? -1 : 1;
//            }
//        }
//    }
//
//    /**
//     * 自定义比较器：按更新时间来排序，置顶优先，降序
//     */
//    static class LatelyUpdateTimeComparator implements Comparator {
//        public int compare(Object object1, Object object2) {
//            Recommend.RecommendBooks p1 = (Recommend.RecommendBooks) object1;
//            Recommend.RecommendBooks p2 = (Recommend.RecommendBooks) object2;
//            if (p1.isTop && p2.isTop || !p1.isTop && !p2.isTop) {
//                return p2.updated.compareTo(p1.updated);
//            } else {
//                return p1.isTop ? -1 : 1;
//            }
//        }
//    }

}